﻿using UnityEngine;
using System.Collections;
using UnityEngine.Networking;

[NetworkSettings(channel =0,sendInterval =0.033f)]
public class Player :NetworkBehaviour {
    public GameObject Spark;

    [SyncVar]
    public Vector3 synsPosition;
    [SyncVar]
    public Vector3 synsEuler;

    public GameObject CloneMonster;
    // Use this for initialization
    void Start () {

        if (isLocalPlayer)
        {
            gameObject.name = "Local Player";
            Manager.GetInstance().NewPlay(this,isServer);
        }
        if (!isLocalPlayer)
        {
            Debug.LogError("not LocalPlayer ");
        }
	
	}

    public GameObject _monster;

    void Update()
    {
        if (isLocalPlayer)
        {
            if (Input.GetKeyDown(KeyCode.U))
            {
                CmdShoot(transform.position,transform.forward);
            }
            if (Input.GetKey(KeyCode.A))
            {
                transform.position = transform.position + Vector3.left * Time.deltaTime;
            }
            if (Input.GetKey(KeyCode.D))
            {
                transform.position = transform.position + Vector3.right * Time.deltaTime;
            }
            if (Input.GetKey(KeyCode.W))
            {
                transform.position = transform.position + Vector3.forward * Time.deltaTime;
            }
            if (Input.GetKey(KeyCode.S))
            {
                transform.position = transform.position + Vector3.back * Time.deltaTime;
            }
            //测试怪物位置与动画同步 （服务端控制，客户端只是能同步）
            if (Input.GetKeyDown(KeyCode.Y))
            {
                GameObject go = Instantiate(CloneMonster);
                go.transform.parent = this.transform;
                go.transform.position = this.transform.position + new Vector3(Random.Range(-5, 5), 1, Random.Range(-2.5f, 2.5f));
                //只能是主机的本地客户端才生效，远程客户端不生效
                NetworkServer.Spawn(go);

                _monster = go;

            }
            if (Input.GetKeyDown(KeyCode.G))
            {
                Animator animator = _monster.GetComponent<Animator>();
                if (animator != null)
                {
                    bool isup = animator.GetBool("isup");
                    if (isup)
                    {
                        animator.SetBool("isup",false);
                    }
                    else
                    {
                        animator.SetBool("isup", true);
                    }

                    bool isdown = animator.GetBool("isdown");
                    if (isdown)
                    {
                        animator.SetBool("isdown", false);
                    }
                    else
                    {
                        animator.SetBool("isdown", true);
                    }
                }

            }
            if (Input.GetKeyDown(KeyCode.H)) {

                Animator animator = _monster.GetComponent<Animator>();
                if (animator != null)
                {
                    bool isdown = animator.GetBool("isdown");
                    if (isdown)
                    {
                        animator.SetBool("isdown", false);
                    }
                    else
                    {
                        animator.SetBool("isdown", true);
                    }
                }
            }

        }
        
    }
    void FixedUpdate()
    {
        if (isLocalPlayer)
        {
            CmdPositionRotations(transform.position,transform.eulerAngles);
        }
        if (!isLocalPlayer)
        {
            RefreshPositionRotation();
        }
    }
    //将自己的位置上传给服务器
    [Command]
    public void CmdPositionRotations(Vector3 _posi,Vector3 _euler)
    {
        //Debug.LogError(gameObject.name + " CmdPositionRotations..");
        synsPosition = _posi;
        synsEuler = _euler;
    }
    [SerializeField]
    int lerpRate = 15;
    void RefreshPositionRotation()
    {
        transform.position = Vector3.Lerp(transform.position, synsPosition, Time.deltaTime * lerpRate);
        transform.eulerAngles = Vector3.Lerp(transform.eulerAngles, synsEuler, Time.deltaTime * lerpRate);
    }

    //所有客户端将开枪时间发送给服务器
    [Command]
    public void CmdShoot(Vector3 positon, Vector3 forawrd)
    {
        //Debug.LogError(gameObject.name + " CmdShoot..");
        Debug.DrawRay(positon, forawrd * 100, Color.red, 0.5f);
        Ray raycast = new Ray(positon, forawrd);
        RaycastHit hit;
        LayerMask layer = 1 << (LayerMask.NameToLayer("Player"));
        bool bHit = Physics.Raycast(raycast, out hit, 10000, layer.value);

        if (bHit)
        {
            Debug.Log(hit.collider.gameObject);
            Player ec = hit.collider.transform.parent.GetComponent<Player>();
            if (ec != null)
            {
                ec.UnderAttack();
                RpcCreateSpark(hit.point);
            }
            else
            {
                Manager.GetInstance().ReStartGame();
            }
        }
    }
    //主机校验之后将需要产生火花的位置发送给客户端
    [ClientRpc]
    void RpcCreateSpark(Vector3 position)
    {
        //Debug.LogError(gameObject.name + " RpcCreateSpark..");
        GameObject go = GameObject.Instantiate(Spark);
        go.transform.position = position;
        Destroy(go, 3);
    }
    public void UnderAttack()
    {
       
    }

}
